﻿using System;
using System.Collections;
using System.IO;
using System.Threading;
using UnityEngine;
using XUtliPoolLib;

namespace XUpdater
{
	// Token: 0x02000024 RID: 36
	internal sealed class XVersion : MonoBehaviour
	{
		// Token: 0x1700001D RID: 29
		// (get) Token: 0x060000DA RID: 218 RVA: 0x00006178 File Offset: 0x00004378
		public static string VERSION_FILE
		{
			get
			{
				return string.Format("manifest.{0}.assetbundle", XSingleton<XUpdater>.singleton.TargetVersion.ToString());
			}
		}

		// Token: 0x060000DB RID: 219 RVA: 0x000061A4 File Offset: 0x000043A4
		public static string GetLocalVersion()
		{
			return XSingleton<XCaching>.singleton.UpdatePath + XVersion.LOCAL_VERSION_FILE;
		}

		// Token: 0x060000DC RID: 220 RVA: 0x000061CC File Offset: 0x000043CC
		public void ServerDownload(HandleVersionDownload callback1, HandleVersionLoaded callback2)
		{
			base.StopAllCoroutines();
			string text = XSingleton<XCaching>.singleton.HostUrl + XSingleton<XUpdater>.singleton.Platform;
			XSingleton<XLoadingUI>.singleton.SetStatus(XSingleton<XStringTable>.singleton.GetString("XUPDATE_INFO_CONNECTING"), byte.MaxValue, byte.MaxValue, byte.MaxValue);
			XSingleton<XDebug>.singleton.AddLog("connecting to update server: ", text, null, null, null, null, XDebugColor.XDebug_None);
			this._time_out_token = XSingleton<XTimerMgr>.singleton.SetTimer(5f, new XTimerMgr.ElapsedEventHandler(this.OnTimeOut), null);
			base.StartCoroutine(this.DownloadVersion(XSingleton<XCaching>.singleton.MakeToken(text + XVersion.VERSION_FILE), callback1, callback2));
		}

		// Token: 0x060000DD RID: 221 RVA: 0x00006280 File Offset: 0x00004480
		private IEnumerator DownloadVersion(string url, HandleVersionDownload callback1, HandleVersionLoaded callback2)
		{
			this._server_Version = new WWW(url);
			yield return this._server_Version;
			XSingleton<XLoadingUI>.singleton.SetStatus(XSingleton<XStringTable>.singleton.GetString("XUPDATE_INFO_CHECKUPDATING"), byte.MaxValue, byte.MaxValue, byte.MaxValue);
			XSingleton<XTimerMgr>.singleton.KillTimer(this._time_out_token);
			//bool flag = this._server_Version != null;
			if (this._server_Version != null)
			{
				//bool flag2 = string.IsNullOrEmpty(this._server_Version.error);
				if (string.IsNullOrEmpty(this._server_Version.error))
				{
					//bool flag3 = callback1 != null;
					if (callback1 != null)
					{
						AssetBundle ab = this._server_Version.assetBundle;
						//bool flag4 = ab == null;
						if (ab == null)
						{
							XSingleton<XDebug>.singleton.AddErrorLog("load server manifest bundle error.", null, null, null, null, null);
							XSingleton<XLoadingUI>.singleton.SetStatus(XSingleton<XStringTable>.singleton.GetString("XUPDATE_ERROR_FETCHMANIFESTERROR"), byte.MaxValue, byte.MaxValue, byte.MaxValue);
							XSingleton<XUpdater>.singleton.OnError();
						}
						else
						{
							UnityEngine.Object asset = ab.LoadAsset("manifest", typeof(TextAsset));
							//bool flag5 = asset == null;
							if (asset == null)
							{
								XSingleton<XDebug>.singleton.AddErrorLog("load server manifest bundle error.", null, null, null, null, null);
								XSingleton<XLoadingUI>.singleton.SetStatus(XSingleton<XStringTable>.singleton.GetString("XUPDATE_ERROR_FETCHMANIFESTERROR"), byte.MaxValue, byte.MaxValue, byte.MaxValue);
								XSingleton<XUpdater>.singleton.OnError();
							}
							else
							{
								AsyncVersionProcessRequest avpr = callback1(asset as TextAsset);
								while (!avpr.IsDone)
								{
									Thread.Sleep(1);
								}
								//bool flag6 = callback2 != null;
								if (callback2 != null)
								{
									callback2(avpr.IsCorrect);
								}
								ab.Unload(false);
								avpr = null;
							}
							asset = null;
						}
						ab = null;
					}
				}
				else
				{
					//bool flag7 = XUpdater.LaunchMode == XLaunchMode.Dev;
					if (XUpdater.LaunchMode == XLaunchMode.Dev)
					{
						XSingleton<XUpdater>.singleton.DevStart();
					}
					else
					{
						XSingleton<XDebug>.singleton.AddErrorLog(this._server_Version.error, null, null, null, null, null);
						XSingleton<XLoadingUI>.singleton.SetStatus(XSingleton<XStringTable>.singleton.GetString("XUPDATE_ERROR_FETCHMANIFESTERROR"), byte.MaxValue, byte.MaxValue, byte.MaxValue);
						XSingleton<XUpdater>.singleton.OnError();
					}
				}
				this._server_Version.Dispose();
				this._server_Version = null;
			}
			else
			{
				XSingleton<XDebug>.singleton.AddErrorLog("ERROR: _server_Version is NULL!", null, null, null, null, null);
				XSingleton<XLoadingUI>.singleton.SetStatus(XSingleton<XStringTable>.singleton.GetString("XUPDATE_ERROR_FETCHMANIFESTERROR"), byte.MaxValue, byte.MaxValue, byte.MaxValue);
				XSingleton<XUpdater>.singleton.OnError();
			}
			yield break;
		}

		// Token: 0x060000DE RID: 222 RVA: 0x000062A4 File Offset: 0x000044A4
		private IEnumerator LocalDownload(XVersion.HandleFinishDownload callback)
		{
			string path = Application.persistentDataPath + XVersion.VERSION_FILE;
			bool flag = !File.Exists(path);
			if (flag)
			{
				bool flag2 = callback != null;
				if (flag2)
				{
					callback(null, null);
				}
			}
			else
			{
				string local_location = "file://" + path;
				WWW localVersion = new WWW(local_location);
				yield return localVersion;
				bool flag3 = callback != null;
				if (flag3)
				{
					callback(localVersion, localVersion.error);
				}
				localVersion.Dispose();
				localVersion = null;
				local_location = null;
				localVersion = null;
			}
			yield break;
		}

		// Token: 0x060000DF RID: 223 RVA: 0x000062BC File Offset: 0x000044BC
		private void OnTimeOut(object o)
		{
			bool flag = XUpdater.LaunchMode == XLaunchMode.Dev;
			if (flag)
			{
				XSingleton<XUpdater>.singleton.DevStart();
				bool flag2 = Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.OSXEditor;
				if (flag2)
				{
					XSingleton<XDebug>.singleton.AddErrorLog("Connect to update server timeout...", null, null, null, null, null);
				}
			}
			else
			{
				XSingleton<XLoadingUI>.singleton.SetStatus(XSingleton<XStringTable>.singleton.GetString("XUPDATE_ERROR_CANNOTCONNECTTOSERVER"), byte.MaxValue, byte.MaxValue, byte.MaxValue);
				XSingleton<XUpdater>.singleton.OnError();
			}
			base.StopAllCoroutines();
			this._server_Version.Dispose();
			this._server_Version = null;
		}

		// Token: 0x040000B2 RID: 178
		public static readonly string LOCAL_VERSION_FILE = "manifest.asset";

		// Token: 0x040000B3 RID: 179
		private WWW _server_Version = null;

		// Token: 0x040000B4 RID: 180
		private uint _time_out_token = 0U;

		// Token: 0x0200028E RID: 654
		// (Invoke) Token: 0x06000DC2 RID: 3522
		private delegate void HandleFinishDownload(WWW www, string error);
	}
}
